export red="\033[31m"
export green="\033[32m"
export yellow="\033[33m"
export blue="\033[34m"
export purple="\033[35m"
export cyan="\033[36m"
export white="\033[37m"
export background="\033[0m"
#Msys 

export HOME=HOME_PATH
Install_NodeJS(){
echo -e ${white}"====="${green}白狐-Script${white}"====="${background}
echo -e ${green}请选择您的NodeJS下载源${background}
echo -e  ${green} 1. ${cyan}Npmmirror [大陆服务器推荐]${background}
echo -e  ${green} 2. ${cyan}Npmjs [国外服务器推荐]${background}
echo "========================="
echo -en ${green}请输入您的选项: ${background};read num
case ${num} in 
  1)
    NpmMirror="https://registry.npmmirror.com"
    NodeJS_URL="https://registry.npmmirror.com/-/binary/node/latest-v18.x/node-v18.19.0-win-x64.zip"
    NodeJS_URL=$(curl ${NodeJS_URL})
    NodeJS_URL=$(echo ${NodeJS_URL#*\"})
    NodeJS_URL=$(echo ${NodeJS_URL%\"*})
    ;;
  2)
    NpmMirror="https://registry.npmjs.org"
    NodeJS_URL="https://nodejs.org/dist/latest-v18.x/node-v18.19.0-win-x64.zip"
    ;;
  *)
    echo -e ${red}输入错误${background}
    exit
    ;;
esac
echo -e ${cyan}正在下载NodeJS 请耐心等待${background}
curl -o NodeJS.zip ${NodeJS_URL}
if [ ! -d node ];then
    mkdir node
fi
echo -e ${cyan}正在解压NodeJS 请耐心等待${background}
unzip -q NodeJS.zip -d node
mv node/$(node) NodeJS
rm -rf node NodeJS.zip
export PATH=$PATH:$HOME/NodeJS
if [ ! -d $HOME/.local/share/pnpm ];then
    mkdir -p $HOME/NodeJS/.local/share/pnpm
fi
export PATH=$PATH:$HOME/NodeJS/.local/share/pnpm
export PNPM_HOME=$HOME/NodeJS/.local/share/pnpm
echo -e ${cyan}正在将NPM默认源更换为 ${yellow}${NpmMirror}${background}
npm config set registry ${NpmMirror}
npm config set registry ${NpmMirror}
npm install -g npm@latest
echo -e ${cyan}正在安装PNPM${background}
until npm install -g pnpm@latest
do
  if [ "${a}" == "3" ];then
    echo -e ${red}错误次数过多 退出${background}
    exit
  fi
  echo -e ${red}PNPM安装失败 ${green}正在重试${background}
  a=$(($a+1))
done
echo -e ${cyan}正在将PNPM默认源更换为 ${yellow}${NpmMirror}${background}
pnpm config set registry ${NpmMirror}
pnpm config set registry ${NpmMirror}
echo -e ${cyan}正在安装PM2${background}
until pnpm install -g pm2@latest
do
  if [ "${a}" == "3" ];then
    echo -e ${red}错误次数过多 退出${background}
    read
    exit
  fi
  echo -e ${red}PM2安装失败 ${green}正在重试${background}
  a=$(($a+1))
  done
echo
echo -e ${cyan}NodeJS NPM PNPM PM2安装完成${background}
}

Install_ffmpeg(){
echo -e ${white}"====="${green}白狐-Script${white}"====="${background}
echo -e ${green}请选择您的ffmpeg下载源${background}
echo -e  ${green} 1. ${cyan}Moeyy [大陆服务器推荐]${background}
echo -e  ${green} 2. ${cyan}Ghproxy [大陆服务器推荐]${background}
echo -e  ${green} 3. ${cyan}Github [国外服务器推荐]${background}
echo "========================="
echo -en ${green}请输入您的选项: ${background};read num
case ${num} in 
  1)
    ffmpegURL="https://github.moeyy.xyz/https://github.com/GyanD/codexffmpeg/releases/download/5.0/ffmpeg-5.0-essentials_build.zip"
    ;;
  2)
    ffmpegURL="https://mirror.ghproxy.com/https://github.com/GyanD/codexffmpeg/releases/download/5.0/ffmpeg-5.0-essentials_build.zip"
    ;;
  3)
    ffmpegURL="https://github.com/GyanD/codexffmpeg/releases/download/5.0/ffmpeg-5.0-essentials_build.zip"
    ;;
  *)
    echo -e ${red}输入错误${background}
    exit
    ;;
esac
echo -e ${cyan}正在下载ffmpeg 请耐心等待${background}
curl -o ffmpeg.zip ${ffmpegURL}
if [ ! -d Ffmpegtp ];then
    mkdir Ffmpegtp
fi
if [ ! -d ffmpeg ];then
    mkdir ffmpeg
fi
echo -e ${cyan}正在解压ffmpeg 请耐心等待${background}
unzip -q ffmpeg.zip -d Ffmpegtp
mv Ffmpegtp/$(ls Ffmpegtp)/bin/* ffmpeg/
rm -rf Ffmpegtp ffmpeg.zip
echo -e ${cyan}ffmpeg ffprobe 安装完成${background}
}

Install_Redis(){
echo -e ${white}"====="${green}白狐-Script${white}"====="${background}
echo -e ${green}请选择您的Redis-Server下载源${background}
echo -e  ${green} 1. ${cyan}Moeyy [大陆服务器推荐]${background}
echo -e  ${green} 2. ${cyan}Ghproxy [大陆服务器推荐]${background}
echo -e  ${green} 3. ${cyan}Github [国外服务器推荐]${background}
echo "========================="
echo -en ${green}请输入您的选项: ${background};read num
case ${num} in 
  1)
    RedisURL="https://github.moeyy.xyz/https://github.com/tporadowski/redis/releases/download/v5.0.14.1/Redis-x64-5.0.14.1.zip"
    ;;
  2)
    RedisURL="https://mirror.ghproxy.com/https://github.com/tporadowski/redis/releases/download/v5.0.14.1/Redis-x64-5.0.14.1.zip"
    ;;
  3)
    RedisURL="https://github.com/tporadowski/redis/releases/download/v5.0.14.1/Redis-x64-5.0.14.1.zip"
    ;;
  *)
    echo -e ${red}输入错误${background}
    exit
    ;;
esac
echo -e ${cyan}正在下载Redis-Server 请耐心等待${background}
curl -o Redis.zip ${RedisURL}
echo -e ${cyan}正在解压Redis-Server 请耐心等待${background}
if [ ! -d Redis-Server ];then
    mkdir Redis-Server
fi
unzip -q Redis.zip -d Redis-Server
rm -rf Redis.zip
echo -e ${cyan}Redis-Server安装完成${background}
}

Install_JRE(){
echo -e ${white}"====="${green}白狐-Script${white}"====="${background}
echo -e ${green}请选择您的JRE17下载源${background}
echo -e  ${green} 1. ${cyan}清华镜像站 [大陆服务器推荐]${background}
echo -e  ${green} 2. ${cyan}Github官网 [国外服务器推荐]${background}
echo "========================="
echo -en ${green}请输入您的选项: ${background};read num
case ${num} in
    1)
        JDKURL="https://mirrors.tuna.tsinghua.edu.cn/Adoptium/17/jre/x64/windows/OpenJDK17U-jre_x64_windows_hotspot_17.0.10_7.zip"
        ;;
    2)
        JDKURL="https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.10+7/OpenJDK17U-jre_x64_windows_hotspot_17.0.10_7.zip"
        ;;
esac
echo -e ${cyan}正在下载JRE17 请耐心等待${background}
curl -o JRE.zip ${JDKURL}
echo -e ${cyan}正在解压JRE17 请耐心等待${background}
if [ ! -d JRE ];then
    mkdir JRE
fi
unzip -q JRE.zip -d JRE
mv JRE/$(ls JRE) JRE17
rm -rf JRE.zip JRE
echo -e ${cyan}JRE17安装完成${background}
}

Install_QSignServer(){
echo -e ${white}"====="${green}白狐-Script${white}"====="${background}
echo -e ${green}请选择您的QSignServer下载源${background}
echo -e  ${green} 1. ${cyan}Gitee [大陆服务器推荐]${background}
echo -e  ${green} 2. ${cyan}Github [国外服务器推荐]${background}
echo "========================="
echo -en ${green}请输入您的选项: ${background};read num
case ${num} in
    1)
        QSignServerURL="https://gitee.com/baihu433/QSignServer"
        ;;
    2)
        QSignServerURL="https://github.com/baihu433/QSignServer"
        ;;
esac
echo -e ${cyan}正在克隆QSignServer仓库 请耐心等待${background}
git clone --depth=1 ${QSignServerURL} QSignServer
echo -e ${cyan}QSignServer仓库克隆完成${background}
}

if [ ! -e NodeJS/bin/node.exe ];then
    Install_NodeJS
fi

if [ ! -e ffmpeg/ffmpeg.exe ];then
    Install_ffmpeg
fi

if [ ! -e Redis-Server/redis-server.exe ];then
    Install_Redis
fi

if [ ! -e JRE17/java.exe ];then
    Install_JRE
fi

if [ ! -e QSignServer/bin/unidbg-fetch-qsign.bat ];then
    Install_QSignServer
fi